<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Demo</title>
  <style>
    body {
      overflow-x: hidden;
    }
    .console-wrapper {
      margin: auto;
      padding: 12px;
      width: 80%;
      background: #eee;
    }
  </style>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
</head>

<body>
  <div id="msg" class="console-wrapper">
      <input type="text" placeholder="通知类型" v-model="msg.text">
      <input type="radio" v-model="msg.type" name="type" value="success" checked />成功
      <input type="radio" v-model="msg.type" name="type" value="warning"  />警告
      <input type="radio" v-model="msg.type" name="type" value="error"  />错误
      <input type="radio" v-model="msg.type" name="type" value="info"  />信息
      <button @click="submit">发送message</button>
    
  </div>
  <div id="notify" class="console-wrapper">
      <input type="text" placeholder="notify通知类型" v-model="notify.text"/>
      <input type="radio" v-model="notify.type" name="type" value="success" checked />成功
      <input type="radio" v-model="notify.type" name="type" value="warning" />警告
      <input type="radio" v-model="notify.type" name="type" value="error" />错误
      <input type="radio" v-model="notify.type" name="type" value="info" />信息
      <button @click="submit">发送notify</button>
  </div>
  <div class="console-wrapper">
    <pre id="console"></pre>
  </div>
  <script src="https://cdn.bootcss.com/socket.io/2.1.0/socket.io.js"></script>
  <script src="https://cdn.bootcss.com/lodash.js/4.17.10/lodash.min.js"></script>
  <script>
    // 不重要的代码，仅展示使用 - start
    const con = document.querySelector('#console');
    const doc = document.documentElement;
    const wh = document.documentElement.clientHeight;

    const _scrollToBottom = (function() {
      return _.throttle(function() {
        doc.scrollTop = doc.scrollHeight;
      }, 100); 
    })();

    const scrollToBottom = function() {
      if (doc.scrollHeight > wh) {
        _scrollToBottom();
      }
    };

    const log = function() {
      let msgList = [].slice.apply(arguments);
      msgList = msgList.map(function(msg) {
        if (typeof msg !== 'object') {
          return msg;
        }
        try {
          return JSON.stringify(msg, null, 2);
        } catch(error) {
          return _.toString(msg);
        }
      });
      con.innerText += new Date().toLocaleString() + ' ' + msgList.join('') + '\n';
      scrollToBottom();
      console.log.apply(null, arguments);
    };

    // 不重要的代码，仅展示使用 - end

    window.onload = function () {
      // init
      const socket = io('/', {

        // 实际使用中可以在这里传递参数
        query:{
          token:'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdHIiOiIxMTAxIiwiaWF0IjoxNTg0Njc0MzMxLCJleHAiOjE1ODcyNjYzMzF9.DP_mK4qirYA9ekajoqluANXmjTVB-iPmLjxak4zFh34'
        }
      });

      socket.on('connect', () => {
        const id = 'uid10086'//socket.id;
        // const id = socket.id;
        log('#connect,', id);

        // 监听自身 id 以实现 p2p 通讯
        socket.on('setUser', msg => {
          console.log(msg)
          socket.on(msg.data.payload.user_id, data => {
            log('#msg,', data);
          })
        });
        socket.on('notify', r=>{console.log('#online,',r)});
      });

      // 接收在线用户信息
      socket.on('online', msg => {
        log('#online,', msg);
      });

      // 系统事件
      socket.on('disconnect', msg => {
        log('#disconnect', msg);
        socket.disconnect(true)
      });

      socket.on('disconnecting', () => {
        log('#disconnecting');
      });

      socket.on('error', () => {
        log('#error');
      });

      window.socket = socket;
    };

  </script>
  <script>
  var msg = new Vue({
    el:'#msg',
    data:{
      msg:{
        text:'',
        type:'success'
      },
    },
    methods:{
      submit:function() {
        socket.emit('exchange', {
          target: 'notify',
          payload: {
            msg : msg.msg.text,type:msg.msg.type
          },
        });
      }
    }
  })
  var notify = new Vue({
    el:'#notify',
    data:{
      notify:{
        text:'',
        type:''
      }
    },
    methods:{
        submit:function() {
          socket.emit('exchange', {
            target: '1102',
            payload: {
              msg : notify.notify.text,type:notify.notify.type
            },
          });
        }
      }
  })
  </script>
</body>

</html>
